import 'package:flutter/material.dart';
import 'package:wechat_assets_picker/wechat_assets_picker.dart';

/*
 * 本地图库的引擎封装,目前用的 wechat_assets_picker 插件
 */
class AlbumEngine {

  /// 选择图片
  static Future<List<AssetEntity>?> selectImage(
    BuildContext context, {
    int maxAssets = 9,
    List<AssetEntity>? selected,
    int filterMinWidth = 100,
    int filterMaxWidth = 100000,
    int filterMinHeight = 100,
    int filterMaxHeight = 100000,
  }) async {

    FilterOptionGroup filterOptions = FilterOptionGroup()
      ..setOption(
        AssetType.image,
        FilterOption(
          sizeConstraint: SizeConstraint(
            minWidth: filterMinWidth,
            maxWidth: filterMaxWidth,
            minHeight: filterMinHeight,
            maxHeight: filterMaxHeight,
          ),
        ),
      );

    return AssetPicker.pickAssets(
      context,
      pickerConfig: AssetPickerConfig(
        requestType: RequestType.image,
        selectedAssets: selected,
        maxAssets: maxAssets,
        filterOptions: filterOptions,
        gridCount: 3,
        pageSize: 30,
      ),
    );
  }

  /// 选择视频
  static Future<List<AssetEntity>?> selectVideo(
    BuildContext context, {
    int maxAssets = 1,
    List<AssetEntity>? selected,
    int filterMinWidth = 100,
    int filterMaxWidth = 100000,
    int filterMinHeight = 100,
    int filterMaxHeight = 100000,
    int filterMaxSeconds = 60,  //默认能选择60秒以内的视频
  }) async {
    FilterOptionGroup filterOptions = FilterOptionGroup()
      ..setOption(
        AssetType.video,
        FilterOption(
            sizeConstraint: SizeConstraint(
              minWidth: filterMinWidth,
              maxWidth: filterMaxWidth,
              minHeight: filterMinHeight,
              maxHeight: filterMaxHeight,
            ),
            durationConstraint: DurationConstraint(max: Duration(seconds: filterMaxSeconds))),
      );

    return AssetPicker.pickAssets(
      context,
      pickerConfig: AssetPickerConfig(
        requestType: RequestType.video,
        selectedAssets: selected,
        maxAssets: maxAssets,
        filterOptions: filterOptions,
        gridCount: 3,
        pageSize: 30,
      ),
    );
  }
}
